package com.ews.onlineexchange.service;

import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.ServiceSupplier;

@Model
@RolesAllowed( "profession" )
public class UnauditCertList extends EntityQuery<QualificationCert> {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;

	@Inject
	UserService					userService;
	@Inject
	QualificationClassService	qualificationClassService;
	private String				supplierName_app;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<QualificationCert> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		restrictions.add( cb.isNull( entity.get( "auditResult" ) ) );
		Join<QualificationCert, ServiceSupplier> supplier = entity.join( "serviceSupplier" );
		addCertRegionRestriction(restrictions,supplier,cb);
		restrictions.add( cb.equal( supplier.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		String supplierName = (String) getParameterValues().get( "supplierName" );
		if ( !StringUtils.isBlank( supplierName ) )
			restrictions.add( cb.like( supplier.get( "cnname" ), getStringRestriction( supplierName ) ) );

		if ( !StringUtils.isBlank( supplierName_app ) )
			restrictions.add( cb.like( supplier.get( "cnname" ), getStringRestriction( supplierName_app ) ) );

		Join<QualificationCert, QualificationClass> qclass = entity.join( "qualificationClass" );
		String qualificationClassId = (String) getParameterValues().get( "qualificationClassId" );
		if ( !StringUtils.isBlank( qualificationClassId ) ) {
			QualificationClass qc = qualificationClassService.find( qualificationClassId );
			restrictions.add( cb.equal( qclass, qc ) );
		}
		return restrictions;
	}

	public String getSupplierName_app() {
		return supplierName_app;
	}

	public void setSupplierName_app( String supplierName_app ) {
		this.supplierName_app = supplierName_app;
	}

	public UnauditCertList() {
		setMaxResults( MAX_RESULTS_NORMAL );
		setOrder( "createDate ASC" );
	}

}
